package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.*;
import com.wzcl.app.dao.repository.*;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.CatalogEvaluateScoreDto;
import com.wzcl.app.model.dto.TaskEvaluateScoreDto;
import com.wzcl.app.service.CatalogEvaluateScoreService;
import com.wzcl.app.utils.DateUtil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2021年01月13日 13:41:00
 */
@Service
public class CatalogEvaluateScoreServiceImpl implements CatalogEvaluateScoreService {
    @Resource
    private AppUserRepository appUserRepository;
    @Resource
    private EssayCourseCatalogRepository catalogRepository;
    @Resource
    private CatalogEvaluateScoreRepository catalogEvaluateScoreRepository;
    @Resource
    private CatalogTaskRepository taskRepository;
    @Resource
    private TaskEvaluateScoreRepository taskEvaluateScoreRepository;

    @Override
    public CatalogEvaluateScore create(CatalogEvaluateScoreDto catalogEvaluateScoreDto) {
        String phone = SecurityContextHolder.getContext().getAuthentication().getName();
        CatalogEvaluateScore catalogEvaluateScore = new CatalogEvaluateScore();
        catalogEvaluateScore.setAppUser(appUserRepository.findByPhone(phone));
        catalogEvaluateScore.setFinishTime(DateUtil.getDateBySystemTime(System.currentTimeMillis()));
        catalogEvaluateScore.setCourseCatalog(catalogRepository.findCatalogById(catalogEvaluateScoreDto.getCatalogId()));
        catalogEvaluateScore.setAccuracy(getCatalogAccuracy(catalogEvaluateScoreDto.getCatalogId()));
        catalogEvaluateScore.setStar(getCatalogStar(catalogEvaluateScore.getAccuracy()));
        //保存章节学习记录
        return catalogEvaluateScoreRepository.save(catalogEvaluateScore);
    }

    @Override
    public RVO getCatalogStarFromCourse(Integer courseId) {
        AppUser appUser = appUserRepository.findByPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        //查询当前课程下的所有章节
        List<EssayCourseCatalog> catalogList = catalogRepository.findAllByCourseId(courseId);
        List<CatalogEvaluateScore> list = new ArrayList<>();
        //获取章节下的评分
        for (int index = 0; index < catalogList.size(); index++) {
            CatalogEvaluateScore catalogEvaluateScore = catalogEvaluateScoreRepository.findEvaluateByCatalogId(catalogList.get(index).getId(), appUser.getId());
            if (catalogEvaluateScore!=null){
                list.add(catalogEvaluateScore);
            }
        }
        return RVO.success(list);
    }

    @Override
    public RVO getTaskStarFromCatalog(Integer catalogId){
        String phone = SecurityContextHolder.getContext().getAuthentication().getName();
        //查询当前章节下所有任务
        List<CatalogTask> taskList = taskRepository.findAllCatalog(catalogId);
        List<TaskEvaluateScore> list = new ArrayList<>();
        //获取章节下所有任务评分
        for (int index = 0; index < taskList.size(); index++) {
            TaskEvaluateScore taskEvaluateScore = taskEvaluateScoreRepository.findByTaskId(taskList.get(index).getId(), phone);
            if (taskEvaluateScore != null){
                list.add(taskEvaluateScore);
            }
        }
        return RVO.success(list);
    }

    /**
     * 获取章节评分
     * @param catalogId 章节id
     * @return 返回章节评分结果
     */
    public Double getCatalogAccuracy(Integer catalogId) {
        String phone = SecurityContextHolder.getContext().getAuthentication().getName();
        //通过章节id查询章节下的所有任务
        List<CatalogTask> tasks = taskRepository.findAllCatalog(catalogId);
        if (tasks.size() == 0){
            return null;
        }
        Integer total = 0;
        //通过任务id查询用户任务评分表信息
        for (int index = 0; index < tasks.size(); index++) {
            TaskEvaluateScore taskEvaluateScore = taskEvaluateScoreRepository.findByTaskId(tasks.get(index).getId(), phone);
            if (taskEvaluateScore != null){
                Integer star = taskEvaluateScore.getStar();
                total+=star;
            }
        }
        //计算章节得分
        Double accuracy = Double.valueOf(total) / Double.valueOf(tasks.size());
        return accuracy;
    }

    /**
     * 计算评分评分
     * @param accuracy 正确率
     * @return 返回得分
     */
    public Integer getCatalogStar(Double accuracy){
        if (accuracy == null){
            return null;
        }else if (accuracy <0.3){
            return 0;
        }else if (accuracy <0.6){
            return 1;
        }else if (accuracy<0.9){
            return 2;
        }else {
            return 3;
        }
    }
}